//
//  WHUseCertModel.m
//  WHPersonalData
//
//  Created by shengmeng chen on 2021/9/4.
//  Copyright © 2021 mac. All rights reserved.
//

#import "WHUseCertModel.h"
//#import "UserLoginModel.h"
#import "WHRunConfigurationModel.h"

@implementation WHUseCertModel

/**
 *  添加模型
 *
 *  @param model             模型
 *  @param succese           添加成功回调
 *  @param failure           添加失败回调
 */
//+(void)addUseComModel:(WHUseCertModel*)model succesefulBlock:(void (^)(WHUseCertModel *model))succese andFailureBlock:(void (^)(NSString *msg))failure
//{
//    [SqliteDBAccess prepareSql:[NSString stringWithFormat:@"INSERT INTO t_use_cert (phone,certtyppe,name,cert,datatime,remark,ext1,ext2,ext3,ext4,ext5) VALUES(?,?,?,?,?,?,?,?,?,?,?);"] fromDatabase:nil succesefulBlock:^(sqlite3_stmt *stmt)
//     {
//         sqlite3_bind_text(stmt,1, model.phone.UTF8String, -1, NULL);
//         sqlite3_bind_text(stmt,2, model.certtyppe.UTF8String, -1, NULL);
//         sqlite3_bind_text(stmt,3, model.name.UTF8String, -1, NULL);
//         sqlite3_bind_text(stmt,4, model.cert.UTF8String, -1, NULL);
//         sqlite3_bind_text(stmt,5, model.datatime.UTF8String, -1, NULL);
//         sqlite3_bind_text(stmt,6, model.remark.UTF8String, -1, NULL);
//         sqlite3_bind_text(stmt,7, model.ext1.UTF8String, -1, NULL);
//         sqlite3_bind_text(stmt,8, model.ext2.UTF8String, -1, NULL);
//         sqlite3_bind_text(stmt,9, model.ext3.UTF8String, -1, NULL);
//         sqlite3_bind_text(stmt,10,model.ext4.UTF8String, -1, NULL);
//         sqlite3_bind_text(stmt,11,model.ext5.UTF8String, -1, NULL);
//
//         if (sqlite3_step(stmt) == SQLITE_DONE)
//         {
//             if (succese)
//             {
//                 succese(model);
//             }
//         }
//         else
//         {
//             if (failure) {
//                 failure([NSString stringWithFormat:@"添加失败，%@", [NSString stringWithUTF8String:sqlite3_errmsg(nil)]]);
//             }
//         }
//         //在遍历完结果集后，调用sqlite3_finalize以释放和预编译的语句相关的资源。
//         //sqlite3_finalize(stmt);
//     } andFailureBlock:^(NSString *msg)
//     {
//         if (failure) {
//             failure([NSString stringWithFormat:@"添加失败，%@", msg]);
//         }
//     }];
//}
//
///**
// *  更新
// *
// *  @param sql          sql语句
// *  @param succese      更新成功回调
// *  @param failure      更新失败回调
// */
//+ (void)updateUseComModelTableWithSql:(NSString*)sql  succesefulBlock:(void (^)(WHUseCertModel *model))succese andFailureBlock:(void (^)(NSString *msg))failure
//{
//    [SqliteDBAccess prepareSql:sql fromDatabase:nil succesefulBlock:^(sqlite3_stmt *stmt) {
//        
//        //执行完成
//        if (sqlite3_step(stmt) == SQLITE_DONE)
//        {
//            if (succese) {
//                
//                WHUseCertModel *model = [[self getWHUseCertModelFromStatement:stmt] lastObject];
//                
//                succese(model);
//            }
//        }
//        else
//        {
//            if (failure) {
//                failure([NSString stringWithFormat:@"更新失败，%@", [NSString stringWithUTF8String:sqlite3_errmsg(nil)]]);
//            }
//        }
//        
//        //在遍历完结果集后，调用sqlite3_finalize以释放和预编译的语句相关的资源。
//        //sqlite3_finalize(stmt);
//    } andFailureBlock:^(NSString *msg) {
//        if (failure) {
//            failure([NSString stringWithFormat:@"更新失败，%@", msg]);
//        }
//    }];
//}
//
////查询
//+ (void)searchWHUseCertModelWithSql:(NSString*)sqlString  succesefulBlock:(void (^)(NSArray *models))succese andFailureBlock:(void (^)(NSString *msg))failure
//{
//    [SqliteDBAccess prepareSql:sqlString fromDatabase:nil succesefulBlock:^(sqlite3_stmt *stmt)
//     {
//         if (succese) {
//             succese([self getWHUseCertModelFromStatement:stmt]);
//         }
//         //在遍历完结果集后，调用sqlite3_finalize以释放和预编译的语句相关的资源。
//         //sqlite3_finalize(stmt);
//     } andFailureBlock:^(NSString *msg) {
//         if (failure) {
//             failure(msg);
//         }
//     }];
//}
//
////查询
//+ (void)searchCountWHUseCertModelWithSql:(NSString*)sqlString  succesefulBlock:(void (^)(int count))succese andFailureBlock:(void (^)(NSString *msg))failure
//{
//    
//    [SqliteDBAccess prepareSql:sqlString fromDatabase:nil succesefulBlock:^(sqlite3_stmt *stmt)
//     {
//         if (succese) {
//             
//             NSArray*array =[self getWHUseCertModelFromStatement:stmt];
//             int count =0 ;
//             if (array.count>0) {
//                 WHUseCertModel*wHUseCertModel =array.firstObject;
//                 count = [wHUseCertModel.phone intValue];
//             }
//             succese(count);
//         }
//         //在遍历完结果集后，调用sqlite3_finalize以释放和预编译的语句相关的资源。
//         //sqlite3_finalize(stmt);
//     } andFailureBlock:^(NSString *msg) {
//         if (failure) {
//             failure(msg);
//         }
//     }];
//}
//
///**
// *  删除模型
// *
// *  @param sql          模型
// *  @param succese      删除成功回调
// *  @param failure      删除失败回调
// */
//+ (void)deleteUseComModelTableWithSql:(NSString *)sql  succesefulBlock:(void (^)(WHUseCertModel *model))succese andFailureBlock:(void (^)(NSString *msg))failure
//{
//    WHUseCertModel *wHUseCertModel=[WHUseCertModel new];
//    [SqliteDBAccess executeSql:sql toDatabase:nil succesefulBlock:^{
//        if (succese) {
//            succese(wHUseCertModel);
//        }
//    } andFailureBlock:^(NSString *msg)
//     {
//         if (failure) {
//             failure(msg);
//         }
//     }];
//}
//
///**
// *  从sqlite3_stmt中获取数据
// *
// *  @param stmt sqlite3_stmt结果集
// *
// *  @return model数组
// */
//+(NSArray *)getWHUseCertModelFromStatement:(sqlite3_stmt *)stmt
//{
//    NSMutableArray *models = [NSMutableArray arrayWithCapacity:1];
//    WHUseCertModel *model;
//    while (sqlite3_step(stmt) == SQLITE_ROW)
//    {
//        model = [[WHUseCertModel alloc] init];
//        char *phone     = (char *)sqlite3_column_text(stmt,0);
//        char *certtyppe = (char *)sqlite3_column_text(stmt,1);
//        char *name      = (char *)sqlite3_column_text(stmt,2);
//        char *cert  = (char *)sqlite3_column_text(stmt,3);
//        char *datatime  = (char *)sqlite3_column_text(stmt,4);
//        char *remark    = (char *)sqlite3_column_text(stmt,5);
//       
//        char *ext1 = (char *)sqlite3_column_text(stmt,6);
//        char *ext2 = (char *)sqlite3_column_text(stmt,7);
//        char *ext3 = (char *)sqlite3_column_text(stmt,8);
//        char *ext4 = (char *)sqlite3_column_text(stmt,9);
//        char *ext5 = (char *)sqlite3_column_text(stmt,10);
//       
//        model.phone      = phone?[NSString stringWithUTF8String:phone] : @"";
//        model.certtyppe  = certtyppe?[NSString stringWithUTF8String:certtyppe] : @"";
//        model.name       = name?[NSString stringWithUTF8String:name] : @"";
//        model.cert       = cert?[NSString stringWithUTF8String:cert] : @"";
//        model.datatime   = datatime?[NSString stringWithUTF8String:datatime] : @"";
//        model.remark     = remark?[NSString stringWithUTF8String:remark] : @"";
//       
//        model.ext1 = ext1?[NSString stringWithUTF8String:ext1] : @"";
//        model.ext2 = ext2?[NSString stringWithUTF8String:ext2] : @"";
//        model.ext3 = ext3?[NSString stringWithUTF8String:ext3] : @"";
//        model.ext4 = ext4?[NSString stringWithUTF8String:ext4] : @"";
//        model.ext5 = ext5?[NSString stringWithUTF8String:ext5] : @"";
//   
//        [models addObject:model];
//    }
//    return models;
//}
//
//+(BOOL)addOrUpdateWHUseComModels:(NSArray *)wHUseComModels withUseCertType:(WHUseCertType)useCertType withName:(NSString *)name
//{
//    __block BOOL re = YES;
//    for(int i = 0;i <wHUseComModels.count;i++)
//    {
//       WHUseCertModel *wHUseCertModel = wHUseComModels[i];
//        [WHUseCertModel checkWHUseComModel:wHUseCertModel withUseCertType:useCertType withName:name SuccesefulBlock:^(NSString * _Nonnull result) {
//            
//           }
//        andFailureBlock:^(NSString *msg) {
//            re = NO;
//        }];
//    }
//    return re;
//}
//
///**********************************手机注册************************************/
//
//+(void)checkWHUseComModel:(WHUseCertModel*)wHUseCertModel withUseCertType:(WHUseCertType)useCertType withName:(NSString *)name SuccesefulBlock:(void (^)(NSString *result))succese andFailureBlock:(void (^)(NSString *msg))failure
//{
//    NSString*sql = @"";
//    NSString *sys = [WHRunConfigurationModel getSystemEnvironment];
//    if (useCertType == WHUseCertPhoneRegiserType)
//    {
//        sql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE phone ='%@' and certtyppe ='%@' and ext3 = '%@';",getUserPhone,wHUseCertModel.certtyppe,sys];
//        if([sys isEqualToString:mOperAppId]){
//            sql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE phone ='%@' and certtyppe ='%@' and (ext3 = '%@' or ext3 = '' or ext3 = null);",getUserPhone,wHUseCertModel.certtyppe,sys];
//        }
//        
//    } else if (useCertType == WHUseCertLegalSpaceType)
//    {
//        sql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE phone ='%@' and certtyppe ='%@' and name ='%@' and ext3 = '%@';",getUserPhone,wHUseCertModel.certtyppe,name,sys];
//        if([sys isEqualToString:mOperAppId]){
//            sql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE phone ='%@' and certtyppe ='%@' and name ='%@' and (ext3 = '%@' or ext3 = '' or ext3 = null);",getUserPhone,wHUseCertModel.certtyppe,name,sys];
//        }
//    }
//    
//    [WHUseCertModel searchWHUseCertModelWithSql:sql succesefulBlock:^(NSArray * _Nonnull models) {
//        if (models.count ==0) {
//            
//            [WHUseCertModel addUseComModel:wHUseCertModel succesefulBlock:^(WHUseCertModel * _Nonnull model) {
//                DebugLog(@"添加证书表成功");
//                succese (@"添加证书表成功");
//            } andFailureBlock:^(NSString * _Nonnull msg) {
//                DebugLog(@"添加证书表失败");
//                failure(@"添加证书表失败");
//            }];
//        
//        }else{
//            
//            NSString*updateSql =[NSString stringWithFormat:@"update t_use_cert set name ='%@',cert='%@',datatime ='%@', remark='%@',ext1='%@',ext2 ='%@',ext3 ='%@',ext4 ='%@',ext5 ='%@' WHERE phone ='%@' and certtyppe ='%@' and ext3 = '%@' ;",wHUseCertModel.name,wHUseCertModel.cert,wHUseCertModel.datatime,wHUseCertModel.remark,wHUseCertModel.ext1,wHUseCertModel.ext2,wHUseCertModel.ext3,wHUseCertModel.ext4,wHUseCertModel.ext5,wHUseCertModel.phone,wHUseCertModel.certtyppe,sys];
//            if([sys isEqualToString:mOperAppId]){
//                updateSql =[NSString stringWithFormat:@"update t_use_cert set name ='%@',cert='%@',datatime ='%@', remark='%@',ext1='%@',ext2 ='%@',ext3 ='%@',ext4 ='%@',ext5 ='%@' WHERE phone ='%@' and certtyppe ='%@' and (ext3 = '%@' or ext3 = '' or ext3 = null) ;",wHUseCertModel.name,wHUseCertModel.cert,wHUseCertModel.datatime,wHUseCertModel.remark,wHUseCertModel.ext1,wHUseCertModel.ext2,wHUseCertModel.ext3,wHUseCertModel.ext4,wHUseCertModel.ext5,wHUseCertModel.phone,wHUseCertModel.certtyppe,sys];
//            }
//            [WHUseCertModel updateUseComModelTableWithSql:updateSql succesefulBlock:^(WHUseCertModel * _Nonnull model)
//            {
//                DebugLog(@"更新证书表成功");
//                succese (@"更新证书表成功");
//            } andFailureBlock:^(NSString * _Nonnull msg) {
//                
//                DebugLog(@"更新证书表失败");
//                failure(@"更新证书失败");
//            }];
//        }
//    } andFailureBlock:^(NSString * _Nonnull msg) {
//        
//    }];
//}
//
//+(BOOL)isExistCertUserRegiser:(NSString*)phone
//{
//        NSString*searchSiteSql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE phone ='%@' and (ext3 = '%@' or ext3 = '' or ext3 = null) and certtyppe in('ce06','ce07','cacert','uspkey' ,'uscert') AND cert IS NOT NULL;",phone,mOperAppId];
//    
//         __block BOOL isExist = YES;
//        [WHUseCertModel searchWHUseCertModelWithSql:searchSiteSql succesefulBlock:^(NSArray * _Nonnull models) {
//            
//            if (models.count >=5)
//            {
//                isExist =  YES;
//            }else{
//              
//                isExist =  NO;
//            }
//        } andFailureBlock:^(NSString * _Nonnull msg) {
//            
//             isExist =  NO;
//        }];
//    return isExist;
//}
//
//+(WHUseCertModel *)getWHUseCertOperModel:(NSString *)certtyppe{
//    NSString* sql =sql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE phone ='%@' and certtyppe ='%@' and (ext3 = '%@' or ext3 = '' or ext3 = null);",getUserPhone,certtyppe,mOperAppId];
//    __block WHUseCertModel* model = [WHUseCertModel new];
//    [WHUseCertModel searchWHUseCertModelWithSql:sql succesefulBlock:^(NSArray * _Nonnull models) {
//        model = models.firstObject;
//    } andFailureBlock:^(NSString * _Nonnull msg) {
//        
//    }];
//    return model;
//}
//
///**********************************法人登陆************************************/
//
//+(void)isExistLegalSpaceLoginWHUseComModelWithName:(NSString*)name succesefulBlock:(void (^)(bool isExist))result
//{
//    NSString *sys = [WHRunConfigurationModel getSystemEnvironment];
//    NSString*searchSiteSql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE phone ='%@' and ext3 = '%@' and certtyppe in('dwcert','dwpkey') and cert IS NOT NULL and name ='%@';",getUserPhone,sys,name];
//    if([sys isEqualToString:mOperAppId]){
//        searchSiteSql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE phone ='%@' and (ext3 = '%@' or ext3 = '' or ext3 = null) and certtyppe in('dwcert','dwpkey') and cert IS NOT NULL and name ='%@';",getUserPhone,sys,name];
//    }
//    
//         __block BOOL isExist = YES;
//        [WHUseCertModel searchWHUseCertModelWithSql:searchSiteSql succesefulBlock:^(NSArray * _Nonnull models) {
//            
//            if (models.count >= 2)
//            {
//                isExist =  YES;
//            }else{
//              
//                isExist =  NO;
//            }
//        } andFailureBlock:^(NSString * _Nonnull msg) {
//            
//             isExist =  NO;
//        }];
//    result(isExist);
//}

+(WHUseCertModel *)getWHUseCertModel:(NSString *)certtyppe{
    NSString *sys = [WHRunConfigurationModel getSystemEnvironment];
    NSString* sql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE phone ='%@' and certtyppe ='%@' and ext3 = '%@';",getUserPhone,certtyppe,sys];
    if([sys isEqualToString:mOperAppId]){
        sql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE phone ='%@' and certtyppe ='%@' and (ext3 = '%@' or ext3 = '' or ext3 = null);",getUserPhone,certtyppe,sys];
    }
    __block WHUseCertModel* model = [WHUseCertModel new];
//    [WHUseCertModel searchWHUseCertModelWithSql:sql succesefulBlock:^(NSArray * _Nonnull models) {
//        model = models.firstObject;
//    } andFailureBlock:^(NSString * _Nonnull msg) {
//        
//    }];
    return model;
}

+(WHUseCertModel *)getWHUseCertModel:(NSString *)certtyppe dept:(NSString *)dept{
    NSString *sys = [WHRunConfigurationModel getSystemEnvironment];
    NSString* sql =[NSString stringWithFormat:@"select *FROM t_use_cert WHERE name = '%@' and phone ='%@' and certtyppe ='%@' and ext3 = '%@';",dept,getUserPhone,certtyppe,sys];
    if([sys isEqualToString:mOperAppId]){
        [NSString stringWithFormat:@"select *FROM t_use_cert WHERE name = '%@' and phone ='%@' and certtyppe ='%@' and (ext3 = '%@' or ext3 = '' or ext3 = null);",dept,getUserPhone,certtyppe,sys];
    }
    __block WHUseCertModel* model = [WHUseCertModel new];
//    [WHUseCertModel searchWHUseCertModelWithSql:sql succesefulBlock:^(NSArray * _Nonnull models) {
//        model = models.firstObject;
//    } andFailureBlock:^(NSString * _Nonnull msg) {
//        
//    }];
    return model;
}

//+(void)delWHUserCertModel:(NSString *)phone sys:(NSString *)sysid succesefulBlock:(void (^)(void))succese andFailureBlock:(void (^)(NSString *msg))failure{
//    NSString* sql =[NSString stringWithFormat:@"delete from t_use_cert where phone ='%@' and ext3 = '%@';",getUserPhone,sysid];
//    [WHUseCertModel deleteUseComModelTableWithSql:sql succesefulBlock:^(WHUseCertModel * _Nonnull model) {
//        succese();
//    } andFailureBlock:^(NSString * _Nonnull msg) {
//        failure(msg);
//    }];
//}
//
//+(void)delAllWHUserCertModel:(NSString *)phone succesefulBlock:(void (^)(void))succese andFailureBlock:(void (^)(NSString *msg))failure{
//    NSString* sql =[NSString stringWithFormat:@"delete from t_use_cert where phone ='%@';",getUserPhone];
//    [WHUseCertModel deleteUseComModelTableWithSql:sql succesefulBlock:^(WHUseCertModel * _Nonnull model) {
//        succese();
//    } andFailureBlock:^(NSString * _Nonnull msg) {
//        failure(msg);
//    }];
//}
@end
